package org.jeecg.modules.report.service.impl;


import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.report.entity.BusinessReport;
import org.jeecg.modules.report.mapper.ReportMapper;
import org.jeecg.modules.report.service.ReportService;
import org.jeecg.modules.report.vo.BusinessReportReq;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    private ReportMapper reportMapper;


    /**
     * 报表统一时间格式
     * */
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    /**
     * 营业统计报表
     */
    @Override
    public List<BusinessReport> businessReportList(BusinessReportReq businessReportReq, HttpServletRequest req) {
        //完善营业报表
        List<BusinessReport> list = new ArrayList<>();
        List<BusinessReport> businessReports = perfectBusinessReports(businessReportReq, reportMapper.businessReportList(businessReportReq));
        if (businessReports!=null&&!businessReports.isEmpty()){
            list.addAll(businessReports);
        }

        //完善营业报表
        List<BusinessReport> cardReport=reportMapper.getCardReport();
        if (!cardReport.isEmpty()){
            for (BusinessReport tb :cardReport ){
                tb.setTime(format.format(new Date()));
            }
            list.addAll(cardReport);
        }
        return list;

    }

    /**
     * 完善营业报表
     */
    private List<BusinessReport> perfectBusinessReports(BusinessReportReq businessReportReq, List<BusinessReport> businessReportList) {
        if (businessReportList.isEmpty()){
            return null;
        }else {
            BusinessReport businessReport = new BusinessReport();
            businessReport.setCourseName("当前营业收入总计");
            if (businessReportReq.getStartDate()!=null &&businessReportReq.getEndDate()!=null){
                businessReport.setTime(format.format(businessReportReq.getStartDate())+"~"+format.format(businessReportReq.getEndDate()));
            }
            for (BusinessReport tb : businessReportList){
                if (tb.getDeposit()==null){
                    tb.setDeposit(new BigDecimal(0));
                }
                if (tb.getCardFee()==null){
                    tb.setCardFee(new BigDecimal(0));
                }
                if (tb.getAccount()==null){
                    tb.setAccount(new BigDecimal(0));
                }
                if (tb.getSum()==null){
                    tb.setSum(new BigDecimal(0));
                }
            }
            for (BusinessReport tb : businessReportList){
                if (tb.getCourseName().equals("会员卡发卡")||tb.getCourseName().equals("会员卡换卡")||
                        tb.getCourseName().equals("会员卡退卡")|| tb.getCourseName().equals("会员支付宝借车扣款")||
                        tb.getCourseName().equals("会员现金充值")||tb.getCourseName().equals("会员充值退款")){
                    if (businessReport.getSum()!=null){
                        if (tb.getSum()!=null){
                            businessReport.setSum(businessReport.getSum().add(tb.getSum()));
                        }else {
                            businessReport.setSum(businessReport.getSum().add(new BigDecimal(0)));
                        }
                    }else {
                        if (tb.getSum()!=null){
                            BigDecimal sum = tb.getSum();
                            businessReport.setSum(sum);
                        }else {
                            businessReport.setSum(new BigDecimal(0));
                        }
                    }
                }
                if (StringUtils.isNotEmpty(businessReport.getTime())){
                    tb.setTime(businessReport.getTime());
                }
            }
            businessReportList.add(businessReport);

            return businessReportList ;
        }
    }


}
